package com.baizhi.service.impl;

import com.baizhi.dao.PmsCategoryMapper;
import com.baizhi.entity.PmsCategory;
import com.baizhi.exception.OrderException;
import com.baizhi.exception.OrderExceptionType;
import com.baizhi.service.AppCategoryService;
import com.baizhi.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppCategoryServiceImpl implements AppCategoryService {
    @Autowired
    private PmsCategoryMapper categoryMapper;

    @Override
    public R getCategoryById(Long categoryId) {
//        先根据一级的id查询二级
        List<PmsCategory> level2 = null;

        if (categoryId == 200){
            throw new OrderException(OrderExceptionType.ORDER_NOT_EXIT.getMsg());
        }

        level2 = categoryMapper.selectList(
                new QueryWrapper<PmsCategory>()
                        .eq("parent_cid", categoryId)
        );


//        根据二级查询三级
        level2 = level2.stream().map(
                pmsCategory -> {
                    List<PmsCategory> level3 = categoryMapper.selectList(
                                new QueryWrapper<PmsCategory>()
                                        .eq("parent_cid", pmsCategory.getCatId())
                        );
                    pmsCategory.setCategoryList(level3);
                    return pmsCategory;
                }
        ).collect(Collectors.toList());

        return R.ok().put("data",level2);
    }
}
